package com.androidhelper.module.common.base;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportFragment;

public abstract class BaseFragment extends SupportFragment {

    // 再点一次退出程序时间设置
    private static final long WAIT_TIME = 2000L;
    private long TOUCH_TIME = 0;

    protected Activity mActivity;
    protected View mRootView;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = getActivity();
    }



    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       // if (mRootView == null) {
            mRootView = inflater.inflate(attachLayoutRes(), container , false);
           mUnbinder = ButterKnife.bind(this, mRootView);

        /*}else{
            ViewGroup parent = (ViewGroup) mRootView.getParent();
            if (parent != null) {
                parent.removeView(mRootView);
            }
        }*/

        initView(mRootView,savedInstanceState);
        setListener();
        return mRootView;
    }




   /* @Override
    public void onViewCreated(@NonNull  View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

         initView(savedInstanceState);
         setListener();
    }*/

    /*@Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
       // initData(); //不需要
    }*/




    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        initEventAndData(savedInstanceState);
    }


    private Unbinder mUnbinder;
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        initLeakCanary();
       // if (useEventBus()) {
       // //    EventBus.getDefault().unregister(this);
      //  }
        if (mUnbinder != null && mUnbinder != Unbinder.EMPTY) {
            mUnbinder.unbind();
        }
        this.mUnbinder = null;
        this.mActivity = null;
        this.mRootView = null;
    }



    /** 返回一个用于显示界面的布局id */
    @LayoutRes
    protected abstract int attachLayoutRes();
    /** 初始化View的代码写在这个方法中 */
    protected abstract void initView(View view, @Nullable Bundle savedInstanceState);
    /** 初始化监听器的代码写在这个方法中 */
    protected abstract void setListener();


    /** 初始数据的代码写在这个方法中，用于从服务器获取数据
     * 初始化加载数据  (非懒加载数据,会同时加载所有的fragment的数据) */
    protected abstract  void initEventAndData(Bundle savedInstanceState);







    /**
     * 回退事件处理
     * @return
     */
    @Override
    public boolean onBackPressedSupport() {
        /* if (getChildFragmentManager().getBackStackEntryCount() > 1) {
            popChild();
        }else {
            doubleClickExit();
        }*/
       return super.onBackPressedSupport();

       /* if (System.currentTimeMillis() - TOUCH_TIME < WAIT_TIME) {
            _mActivity.finish();
        } else {
            TOUCH_TIME = System.currentTimeMillis();
            ToastUtils.(_mActivity, R.string.press_again_exit, Toast.LENGTH_SHORT).show();
        }
        return true;*/


    }










    /**
     * 用来检测所有Fragment的内存泄漏
     */
    private void initLeakCanary() {
     //   RefWatcher refWatcher = LibApplication.getRefWatcher(mActivity);
     //   refWatcher.watch(mActivity);
    }





}
